package com.wwdx.controller.admin;

import com.wwdx.controller.base.BaseController;
import com.wwdx.model.WwdxMerchantProductModel;
import com.wwdx.model.WwdxMerchantProductStatisticsModel;
import com.wwdx.model.dto.ResultBean;
import com.wwdx.service.WwdxMerchantProductServiceI;
import com.wwdx.shiro.SessionUser;
import com.wwdx.utils.ConstantHolder;
import com.wwdx.utils.QrCodeUtil;
import me.chanjar.weixin.common.util.http.URIUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.List;

/**
 * @author liguoliang
 * @date 2018/1/31 18:44
 */
@Controller
@RequestMapping("/system/wwdx_merchant_product")
public class WwdxMerchantProductController extends BaseController {

    @RequestMapping("/list")
    public ModelAndView index(WwdxMerchantProductModel model,
                              @RequestParam(value = "page", defaultValue = "1") Integer page,
                              @RequestParam(value = "rows",defaultValue = "10") Integer rows) {

        ModelAndView mav = new ModelAndView();
        String merchantId = model.getMerchant_id();
        if (StringUtils.isBlank(merchantId)){
            model.setMerchant_id(SessionUser.getMerchantId());
        }
        mav.addObject("listSize", wwdxMerchantProductService.selectModelCount(model));
        mav.addObject("list", wwdxMerchantProductService.selectModelList(model,page,rows));
        return mav;
    }

    @RequestMapping("/statistics_list")
    public ModelAndView list(WwdxMerchantProductStatisticsModel model,
                             @RequestParam(value = "page", defaultValue = "1") Integer page,
                             @RequestParam(value = "rows", defaultValue = "10") Integer rows) {

        ModelAndView mv = new ModelAndView();
        String merchantId = model.getMerchant_id();
        if (StringUtils.isBlank(merchantId)){
            model.setMerchant_id(SessionUser.getMerchantId());
        }
        mv.addObject("listSize",  wwdxMerchantProductStatisticsService.selectModelCount(model));
        mv.addObject("list", wwdxMerchantProductStatisticsService.selectModelList(model,page,rows));
        mv.addObject("model",model);
        return mv;
    }

    /**
     * 产品列表
     */
    @RequestMapping("/list/json")
    @ResponseBody
    public ResultBean listjson(String merchant_id){
        List<WwdxMerchantProductModel> list = wwdxMerchantProductService.list(merchant_id);
        List<WwdxMerchantProductServiceI.WwdxMerchantProductDTO> productList = wwdxMerchantProductService.getProductList(list,wwdxThemeService, wwdxVipProductService);
        return renderSuccess(productList);
    }

    /**
     * 保存新增
     */
    @RequestMapping("/save")
    public String save(WwdxMerchantProductModel model)throws Exception{
        Date date = new Date();
        Long productId = model.getProduct_id();
        String merchantId = model.getMerchant_id();
        WwdxMerchantProductModel wwdxMerchantProductModel = wwdxMerchantProductService.isExits(merchantId, productId);
        if (wwdxMerchantProductModel != null){
            return "redirect:/system/wwdx_merchant_product/list";
        }
        //产品链接地址
        String productUrl;
        if (model.getType() == 1){
            //会员页面地址
            productUrl = ConstantHolder.Constant.VIP_PAGE + "?merchant_id="+merchantId;
        }else { 
            //主题页面地址
            productUrl = ConstantHolder.Constant.THEME_PAGE + "?themeId="+productId+"&merchant_id="+merchantId;
        }
        
        //将http链接进行转义
        String url = URIUtil.encodeURIComponent(wechatUrl + productUrl);
        //产品链接
        String orginal = wechatUrl+"auth.html?url="+ url;
        //生成产短链接
        String shortUrl = miDuWxService.shortUrl(orginal);
        model.setProduct_url(shortUrl);
        //二维码图片名称
        String qrcodeUrl = merchantId + productId+ ".jpg";
        QrCodeUtil.encode2(shortUrl,"/data/wwdx/static/activity/merchant",qrcodeUrl);
        model.setQrcode_url(picUrl+"activity/merchant/"+qrcodeUrl);
        model.setGmt_created(date);
        model.setGmt_modified(date);
        wwdxMerchantProductService.save(model);
        return "redirect:/system/wwdx_merchant_product/list";
    }

    /**
     * 删除
     */
    @RequestMapping("/del")
    public String del(Long id){
        wwdxMerchantProductService.delete(wwdxMerchantProductService.getById(id));
        return "redirect:/system/wwdx_merchant_product/list";
    }
}
